package view.component.monster.large_monster
{
	import embeds.EmbedRes;
	
	import enums.main.UISize;
	
	import feathers.controls.Button;
	import feathers.data.ListCollection;
	
	import model.vo.monster.SpVo;
	
	import starling.display.Image;
	import starling.events.Event;
	import starling.textures.Texture;
	
	import util.FG2FUGenerator;
	
	import view.component.BaseListView;
	import view.event.monster.LMVideoListViewEvent;
	import view.render.monster.LMonsterVideoListItem;
	
	
	/**
	 * 大型怪物详细-视频收藏-列表界面
	 * @author £iang
	 * 
	 */	
	public class LMVideoListView extends BaseListView
	{
		//-----------------------------
		// variables
		//-----------------------------
		
		/** 添加按钮 */
		public var addBtn:Button;
		
		//-----------------------------
		// properties
		//-----------------------------
		
		/** 列表中上次选中的选项下标 */
		private var _lastSelectIndex:int = -1;
		
		//-----------------------------
		// setter&getter
		//-----------------------------
		
		//-----------------------------
		// methods
		//-----------------------------
		
		/**
		 * 初始化函数 
		 * 
		 */		
		override protected function initialize():void
		{ 
			_itemH = UISize.LMONSTER_VIDEO_ITEM_H * this.dpiScale;
			_listH = UISize.MAIN_H - 
				( UISize.MONSTER_TAB_AREA_H + 
					UISize.LMONSTER_DETAIL_TAB_AREA_H +
					UISize.LIST_TITLE_H ) * this.dpiScale;
			_firstName = "";
			super.initialize();
			addListener();
		}
		
		override protected function initializeOther():void
		{
			var tmpTexture:Texture = Texture.fromBitmapData( new EmbedRes.ADD_ICON().bitmapData );
			var tmpImage:Image = new Image( tmpTexture );
			
			tmpImage.scaleX = tmpImage.scaleY = this.dpiScale;
			addBtn = FG2FUGenerator.generateRoundRectButton(
				120, UISize.LIST_TITLE_H, this.dpiScale, 12, "small"
			);
			addBtn.label = "添加";
			addBtn.defaultIcon = tmpImage;
			titleGroup.addChild( addBtn );
		}
		
		/**
		 * 绘制函数 
		 * 
		 */		
		override protected function draw():void
		{
			list.y = UISize.LIST_TITLE_H * this.dpiScale;
		}
		
		/**
		 * 设置列表数据 
		 * @param dataVec 数据
		 * 
		 */		
		public function setListData( dataVec:Vector.<SpVo> ):void
		{
			var i:int;
			var len:int = dataVec.length;
			
			_listData = new ListCollection([]);
			
			for( i = 0; i < len; i++)
			{
				var item:LMonsterVideoListItem = new LMonsterVideoListItem();
				var tmpData:SpVo = dataVec[ i ] as SpVo;
				
				item.setData( tmpData );
				_listData.push( { item:item, itemData:tmpData } );
			}
			
			list.dataProvider = _listData;
		}
		
		/**
		 * 添加侦听 
		 * 
		 */		
		private function addListener():void
		{
			list.addEventListener( Event.CHANGE, listChangeHandler );
			addBtn.addEventListener( Event.TRIGGERED, addTriggeredHandler );
		}
		
		/**
		 * 初始化列表 
		 * 
		 */		
		public function initData():void
		{
			_lastSelectIndex = -1;
			disposeItems();
		}
		
		//-----------------------------
		// events
		//-----------------------------
		
		/**
		 * 处理列表选中某项后派发的事件 
		 * @param event 列表选中某项后派发的事件 
		 * 
		 */		
		protected function listChangeHandler( event:Event ):void
		{
			if ( list.selectedIndex >= 0 )
			{
				if ( _lastSelectIndex >= 0 )
					( _listData.data[ _lastSelectIndex ].item as LMonsterVideoListItem ).changeScrollerPage( 0 );
				
				( _listData.data[ list.selectedIndex ].item as LMonsterVideoListItem ).changeScrollerPage( 1 );
				_lastSelectIndex = list.selectedIndex;
			}
		}
		
		/**
		 * 处理点击添加按钮后派发的事件 
		 * @param event 点击添加按钮后派发的事件 
		 * 
		 */		
		protected function addTriggeredHandler( event:Event ):void
		{
			dispatchCommonEvent(
				new LMVideoListViewEvent( LMVideoListViewEvent.START_ADD )
			);
		}
		
		//-----------------------------
		// dispose
		//-----------------------------
		
		/**
		 * 析构函数 
		 * 
		 */		
		override protected function disposeOther():void
		{
			addBtn.dispose();
			FG2FUGenerator.deepDisposeButtonComp( addBtn );
			addBtn = null;
		}
		
		//-----------------------------
		// construction
		//-----------------------------
		
		/**
		 * 构造函数
		 *
		 */
		public function LMVideoListView()
		{
			super();
		}
	}
}